package com.ssi.myshop.base;

import android.util.Log;

import java.util.List;

import io.reactivex.observers.ResourceObserver;

public abstract class BaseListObserver<T> extends ResourceObserver<BaseListData<T>> {
    private static final String TAG = "BaseListObserver";

    private IBaseCallBack mCallBack;

    public BaseListObserver(IBaseCallBack callBack) {
        mCallBack = callBack;
    }

    @Override
    protected void onStart() {
        mCallBack.onLoading();
        super.onStart();
    }

    @Override
    public void onNext(BaseListData<T> baseListData) {
        if (baseListData.getCode() == 0) {
            List<T> data = baseListData.getData();
            Log.d(TAG, "onNext: "+data.toString());
            onSuccess(data);
            mCallBack.onSuccess();
        } else {
            mCallBack.onError(baseListData.getMsg());
        }
    }

    protected abstract void onSuccess(List<T> data);


    @Override
    public void onError(Throwable e) {
        mCallBack.onError(e.getMessage());
        Log.d(TAG, "onError: "+e.getMessage());
    }

    @Override
    public void onComplete() {

    }
}
